//
//  YHYRightView.m
//  self
//
//  Created by 杨泓晔 on 16/4/7.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYRightView.h"
#import "Masonry.h"
//#import "YHYAddressCollectionViewCell.h"

@implementation YHYRightViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.addressL = [UILabel new];
        [self.contentView addSubview:self.addressL];
        [self.addressL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self);
            make.height.width.mas_equalTo(self);
            
        }];
        
        //裁掉边框
        _addressL.layer.masksToBounds = YES;
        //设置半径
       // _addressL.layer.cornerRadius = 7;
        _addressL.textAlignment = NSTextAlignmentCenter;
            //  _addressL.layer.borderWidth = 0.7;
        _addressL.font = [UIFont fontWithName:@"Arial" size:15 * AUTO_WIDTH];
        _addressL.textColor = ZI;
        self.contentView.backgroundColor = LV;
        _addressL.backgroundColor = LV;
        
    }
    
    return self;
}

@end


@interface YHYRightView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)NSMutableArray *dataSource;
@property (nonatomic,retain)NSMutableArray *indexSource;
@end

@implementation YHYRightView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
        self.dataSource = [[NSMutableArray alloc] init];
        self.indexSource = [[NSMutableArray alloc] init];
        [self getData];

    }
    return self;
}

- (void)getData{
    
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"citySort.plist" ofType:nil];
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:plistPath];
    self.dataSource = [self sortArray:[NSMutableArray arrayWithArray:array]];
    
}
//排序并按首字母分组
-(NSMutableArray *)sortArray:(NSMutableArray *)arrayToSort {
    NSMutableArray *arrayForArrays = [[NSMutableArray alloc] init];
    
    //根据拼音对数组排序
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinyin" ascending:YES]];
    //排序
    [arrayToSort sortUsingDescriptors:sortDescriptors];
    
    NSMutableArray *tempArray = nil;
    BOOL flag = NO;
    
    //分组
    for (int i = 0; i < arrayToSort.count; i++) {
        NSString *pinyin = [arrayToSort[i] objectForKey:@"pinyin"];
        NSString *firstChar = [pinyin substringToIndex:1];
        if (![_indexSource containsObject:[firstChar uppercaseString]]) {
            [_indexSource addObject:[firstChar uppercaseString]];
            tempArray = [[NSMutableArray alloc] init];
            flag = NO;
        }
        if ([_indexSource containsObject:[firstChar uppercaseString]]) {
            [tempArray addObject:arrayToSort[i]];
            if (flag == NO) {
                [arrayForArrays addObject:tempArray];
                flag = YES;
            }
        }
    }
    
    return arrayForArrays;
}



- (void)creatView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _rightCollection = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
    [self addSubview:_rightCollection];
    [_rightCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(self);
        
    }];
    _rightCollection.backgroundColor = [UIColor whiteColor];
    _rightCollection.delegate = self;
    _rightCollection.dataSource = self;
    _rightCollection.allowsMultipleSelection = YES;
    
    layout.itemSize = CGSizeMake(20, 20);
    // layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //  layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 2;
    
    
    [_rightCollection registerClass:[YHYRightViewCell class] forCellWithReuseIdentifier:@"YHYAddressCollectionViewCell"];
   
    self.backgroundColor = LV;
    _rightCollection.backgroundColor = LV;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return self.dataSource.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ce = @"YHYAddressCollectionViewCell";
    YHYRightViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ce forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor yellowColor];
    cell.addressL.text = [self.indexSource objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.myDelegate presentindexPath:indexPath];
    
   
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
